package cn.hwadee.muying.service;

import cn.hwadee.muying.commons.base.BaseService;
import cn.hwadee.muying.commons.page.MyPage;
import cn.hwadee.muying.commons.utils.Md5Utils;
import cn.hwadee.muying.entity.ClientInfo;
import cn.hwadee.muying.entity.Reception;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.enums.Status;
import cn.hwadee.muying.mapper.UserMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户相关信息 服务实现类
 * </p>
 *
 * @author Tony
 * @since 2018-04-17
 */
@Service
public class UserService extends BaseService<UserMapper,User> {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ClientInfoService clientInfoService;
    @Autowired
    private ReceptionService receptionService;
    public User getByAccount(String account) {
        return userMapper.selectByAccount(account);
    }

    public MyPage<User> list(MyPage<User> page, User cUser, String keyword) throws Exception{
        List<User> users = userMapper.list(page,cUser,keyword);
        page.setRecords(users);
        return page;
    }

    public void insertUser(User user, User cUser) throws Exception{
        user.setOrgId(cUser.getOrgId());
        user.setPassword(new Md5Utils().getkeyBeanofStr(user.getPassword()));
        super.insert(user);
    }

    public void updatePw(User user, String oldPw, String newPw, String repeatPw) throws Exception{
        if(!newPw.equals(repeatPw)){
            throw new RuntimeException("两次输入的新密码不同");
        }
        if(!user.getPassword().equals(new Md5Utils().getkeyBeanofStr(oldPw))){
            throw new RuntimeException("旧密码不正确");
        }else if(user.getPassword().equals(new Md5Utils().getkeyBeanofStr(newPw))){
            throw new RuntimeException("新密码不能与旧密码相同");
        }else {
            user.setPassword(new Md5Utils().getkeyBeanofStr(newPw));
            super.updateById(user);
        }
    }


    public User getByOpenId(String openid) {

        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.eq(User.VALID, true).eq(User.OPENID, openid);
        List<User> list = userMapper.selectList(ew);
        if(list.isEmpty()){
            return null;
        }

        return list.get(0);

    }


    /**
     * 根据机构获取所有用户
     * @param orgId
     * @return
     */
    public List<User> listByOrg(Long orgId) {
        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.eq(User.ORGID, orgId).eq(User.VALID, true).isNotNull(User.OPENID).ne(User.OPENID, "");
        List<User> users = userMapper.selectList(ew);
        return users;
    }

    public List<User> selectAll() {
        return userMapper.selectList(
                new EntityWrapper<User>()
                        .and("valid=1")
        );
    }

    public List<User> selectAllByRole(Long rid) {
        return userMapper.selectList(
                new EntityWrapper<User>()
                        .and("rid="+rid)
                        .and("valid=1")
        );
    }


    public User getByAccountAndTelephone(String account, String telephone) {
        return userMapper.selectByAccountAndTelephone(account,telephone);
    }

    public int selectCountByRid(Long rid) {
        return userMapper.selectCount(
                new EntityWrapper<User>()
                        .and("rid="+rid)
                        .and("valid=1")
        );
    }

    public User selectByAccount(String account) {
        List<User> userList= userMapper.selectList(new EntityWrapper<User>()
                            .and("account="+account)
                            .and("valid=1")
        );
        if(userList.size()>0){
            return userList.get(0);
        }else{
            return null;
        }
    }

    public void register(User user, ClientInfo clientInfo) {
        this.insert(user);
        user=this.selectByAccount(user.getAccount());
        Reception reception=receptionService.selectByAccount(user.getAccount());
        if(reception!=null){
            //如果该账号对应的接待情况中的推荐职员存在,则设置当前客户的推荐职员id
            clientInfo.setPresenterId(reception.getFollowerId());
            reception.setStatus(Status.FINISH_REGISTER);
            //更新接待情况的状态
            receptionService.updateById(reception);
        }
        clientInfo.setUserId(user.getId());
        clientInfo.setOld(false);
        clientInfoService.insert(clientInfo);
    }
}
